#include<iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
using namespace std;
class Solution {
public:
    int longestValidParentheses(string s) {
        stack<int>sta;
        int max = 1, ykh=1;
        sta.push(-1);
        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] == '(')sta.push(i);
            else
            {
                if (sta.size() > ykh)sta.pop();
                else
                {
                    ykh++;
                    sta.push(i);
                }
            }
        }
        int syg = s.length();
        while (!sta.empty())
        {
            max = max > syg - sta.top() ? max : syg - sta.top();
            syg = sta.top();
            sta.pop();
        }
        return max - 1;
    }
};
int main()
{
    Solution sol;
}